import React, { Component, PureComponent } from 'react'

import { EventEmitter } from 'events'

// 事件总线：event bus
const eventBus = new EventEmitter()


class Home extends PureComponent {

  // 添加时间监听
  componentDidMount() {
    eventBus.addListener("hello", this.handleSayHelloListen)
  }
  // 取消事件监听
  componentWillUnmount() {
    eventBus.removeListener("hello")
  }
  handleSayHelloListen(...args) {
    console.log(args)
  }

  render() {
    return (
      <div>
        Home

      </div>
    )
  }
}

class Profile extends PureComponent {
  render() {
    return (
      <div>
        Profile
        <button onClick={() => { this.emitEvent() }}>点击Profile按钮</button>
      </div>
    )
  }

  emitEvent() {
    eventBus.emit("hello", "你好", 18)
  }
}

export default class App extends Component {
  render() {
    return (
      <div>
        <Home/>
        <Profile/>
      </div>
    )
  }
}
